package com.lyj.threads;

import com.lyj.units.Check;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 线程池管理工具
 * 可在启动的时候初始线程池大小
 */
public class ThreadPoolManage {

    Logger logger = LoggerFactory.getLogger(ThreadPoolManage.class);
    /**
     * 线程池对象
     */
    private static Map<String, SinglePoolParam> instanceMap = new HashMap<>();

    /**
     * 重置线程池参数
     *
     * @param poolName
     * @param targetPoolSizeStr
     * @param targetMaxTaskSize
     */
    public static void resetPool(String poolName, String targetPoolSizeStr, String targetMaxTaskSize) {
        if (Check.isDigit(targetPoolSizeStr) && Check.isDigit(targetMaxTaskSize) ) {
            try {
                SinglePoolParam param = instanceMap.get(poolName);
                if (param.getMaxTaskSize() != Integer.valueOf(targetMaxTaskSize)) {
                    param.setMaxTaskSize(Integer.valueOf(targetMaxTaskSize));
                }
                if (param.getPoolSize() != Integer.valueOf(targetPoolSizeStr)) {
                    param.setPoolSize(Integer.valueOf(targetPoolSizeStr));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 初始化线程池
     * @param poolName
     */
    public static void initPoolParam(String poolName) {
        if (Check.isNull(instanceMap.get(poolName))) {
            instanceMap.put(poolName , new SinglePoolParam(poolName));
        }
    }

    /**
     * 初始化线程池
     * @param poolName 线程池名称
     * @param poolSize  核心线程数
     * @param maxTaskSize 最大线程数
     * @param workQueueSize 任务队列数
     */
    public static void initPoolParam(String poolName, String poolSize, String maxTaskSize, String workQueueSize) {
        if (Check.isNull(instanceMap.get(poolName))){
            instanceMap.put(poolName, new SinglePoolParam(poolName,Integer.valueOf(poolSize), Integer.valueOf(maxTaskSize),Integer.valueOf(workQueueSize)));
            System.out.println("初始化线程池：" + poolName + " core：" + poolSize + " max：" + maxTaskSize + " queue：" + workQueueSize);
        }
    }

    /**
     * 获取线程池单例
     * @param poolName
     * @return
     */
    public static synchronized SinglePoolParam getInstance(String poolName) {
        SinglePoolParam pool = instanceMap.get(poolName);
        if (Check.isNull(pool)) {
            initPoolParam(poolName);
        }
        return instanceMap.get(poolName);
    }

    /**
     * 获取当前线程池
     * @return
     */
    public static Map<String , SinglePoolParam> getInstanceMap() {
        return instanceMap;
    }

    public static void shutDown(String poolName) {
        SinglePoolParam pool = instanceMap.get(poolName);
        if (!Check.isNull(pool)) {
            // 停止线程池
            pool.getExecutor().shutdown();
        }
    }

    /**
     * 销毁线程池
     */
    public static void shutDownPool() {
       if (instanceMap != null) {
           Iterator<String> iterator = instanceMap.keySet().iterator();
           String next = null;
           while (iterator.hasNext()) {
               next = iterator.next();
               try {
                   instanceMap.get(next).getExecutor().shutdown();
               } catch (Exception e) {
                   System.out.println(next + "线程池销毁异常" + e);
                   e.printStackTrace();
               }
           }
       }
    }
}
